from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
from HexMap import *
from random import *
from copy import *

class MapGenerator:
    """Class used to generate hexagonal maps"""

    def __init__(self):
        self.heightTab = []
    
    def LoadHeightmap(self, filepath):
        self.heightmap = PNMImage(filepath)

    screenX = 12
    screenY = 20

    def GenerateMap(self, filepath, hex, scale, singleHexSize):
        self.LoadHeightmap(filepath)

        sandTexture = loader.loadTexture("textures/sand.dds")
        grassTexture = loader.loadTexture("textures/grass.dds")
        rockTexture = loader.loadTexture("textures/rock.dds")
        snowTexture = loader.loadTexture("textures/snow.dds")
        tree1Model = loader.loadModel("Models/tree1/tree1.egg")
        tree2Model = loader.loadModel("Models/tree2/tree2.egg")
        churchillModel = loader.loadModel("Models/churchill/churchill.egg")
        tigerModel = loader.loadModel("Models/tiger/tiger.egg")
        spitfireModel = loader.loadModel("Models/spitfire/spitfire.egg")
        #tree1Model.setHpr(0,90.0,0)
        tree1Model.setScale(0.5, 0.5, 0.5)
        #tree2Model.setHpr(0,90.0,0)
        tree2Model.setScale(0.5, 0.5, 0.5)

        x = self.heightmap.getReadXSize()
        y = self.heightmap.getReadYSize()

        hexMap = HexMap()
        geom = self.CreateHex();
        #node = GeomNode("Tile")
        #node.addGeom(geom)   
        #hexagon = hex.attachNewNode(node)                                            
        #hexagon.setPos(0,0,0)

        for i in range(0, x):
            for j in range(0, y):
                node = GeomNode(str(i)+'-'+str(j))
                node.addGeom(geom)
                node2 = hex.attachNewNode(node)
                if (random()<0.2 and j % 4 == 0 and i%4 == 0 and (i<32 or i>96) ):
                    copy(tigerModel).reparentTo(node2)
                    

                #temp = hex.attachNewNode(str(i)+'-'+str(j))

                z = float(self.heightmap.getGrayVal(i, j))*float(scale)
                hexMap.CreateHex(i, j, z, scale)
                self.heightTab.append(z)
                if i % 2 == 0:
                    node2.setPos(j, i*singleHexSize, z)
                else:
                    node2.setPos(j+0.5, i*singleHexSize, z)


                if z<1:
                    node2.setTexture(sandTexture);
                elif z<2:
                    node2.setTexture(grassTexture);
                    if random()<0.1:
                        copy(tree1Model).reparentTo(node2)
                    elif random()<0.1:
                        copy(tree2Model).reparentTo(node2)
                elif z<3:
                    node2.setTexture(rockTexture);
                else:
                    node2.setTexture(snowTexture);
        #hex.flattenStrong()
        return hexMap

    vertices = [Vec3(0, 0.5, 0), Vec3(0.433, 0.258, 0), Vec3(0.433, -0.242, 0), Vec3(0, -0.492, 0),
                Vec3(-0.433, -0.242, 0), Vec3(-0.433, 0.258, 0), (0, 0, 0)]

    vertices2 = [Vec3(0, 0, 1), Vec3(0.866, 0, 0.5), Vec3(0.866, 0, -0.5), Vec3(0, 0, -1),
                Vec3(-0.866, 0, -0.5), Vec3(-0.866, 0, 0.5), Vec3(0, 0, 0)] 
    texcoords= [Vec2(0.501, -0.493), Vec2(-0.360, 0.003), Vec2(-0.361, 0.996), Vec2(0.5, 1.5),
                Vec2(1.360, 0.997), Vec2(1.361, 0.004), Vec2(0.5, 0.5)]

    def CreateHex(self):
        format = GeomVertexFormat.getV3t2()
        geomData = GeomVertexData("hex", format, Geom.UHStatic)
        vertexWriter = GeomVertexWriter(geomData, "vertex")
        uvWriter = GeomVertexWriter(geomData, "texcoord")

        for pos, tex in zip(self.vertices, self.texcoords):
            vertexWriter.addData3f(pos)
            uvWriter.addData2f(tex)

        triangles = GeomTriangles(Geom.UHStatic)
        triangles.addVertices(1, 0, 6)
        triangles.closePrimitive()
        triangles.addVertices(2, 1, 6)
        triangles.closePrimitive()
        triangles.addVertices(3, 2, 6)
        triangles.closePrimitive()
        triangles.addVertices(4, 3, 6)
        triangles.closePrimitive()
        triangles.addVertices(5, 4, 6)
        triangles.closePrimitive()
        triangles.addVertices(0, 5, 6)
        triangles.closePrimitive()

        geom = Geom(geomData)
        geom.addPrimitive(triangles)
        return geom







